package com.android.pdd.baseapp.hook.widget;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.pdd.baseapp.hook.ViewCreater;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseFloatMainView extends BaseFloatHookView implements View.OnClickListener {

    private LinearLayout tabLayout;

    private FrameLayout fragmentContainer;

    public BaseFloatMainView(Context context) {
        super(context);
        init();
    }

    private void init() {
        tabLayout = new ViewCreater.LinearLayoutBuilder(context)
                .setOrientationHorizontal()
                .setWidthMatch()
                .setPadding(0, ConvertUtils.dp2px(12), 0, ConvertUtils.dp2px(12))
                .build();
        addView(tabLayout);

        fragmentContainer = new FrameLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ScreenUtils.getScreenHeight() / 3);
        int id = 1101101101;
        fragmentContainer.setId(id);
        fragmentContainer.setVisibility(GONE);
        addView(fragmentContainer, params);
        initTabs();
        initTabsData();

    }

    @Override
    protected int exceptTopHeight() {
        return tabLayout.getHeight();
    }

    private void initTabsData() {
        for (TabItem tabItem : tabItemList) {
            int marginLeft = tabItem.index != 0 ? ConvertUtils.dp2px(8) : 0;
            Log.e("=======", "marginLeft:" + marginLeft);
            TextView tab = new ViewCreater.TextViewBuilder(context)
                    .setText(tabItem.tabName)
                    .setTextColor(0xffffffff)
                    .setTextSize(15)
//                    .setMargin(marginLeft, 0, 0, 0)
                    .setPadding(marginLeft, 0, 0, 0)
                    .setOnClickListener(this)
                    .build();
            tab.setTag(tabItem);
            tabItem.setBindTextView(tab);
            tabLayout.addView(tab);
        }
    }

    protected abstract void initTabs();

    //    private List<Class> fragmentClassList = new ArrayList<>();

    private Map<String, Fragment> fragmentMap = new HashMap<>();
    protected List<TabItem> tabItemList = new ArrayList<>();

    protected void addTab(String tabName) {
        addTab(tabName, null);
    }

    protected void addTab(String tabName, Class fragmentClass) {
        TabItem tabItem = new TabItem();
        tabItem.setTabName(tabName);
        tabItem.setFragmentClass(fragmentClass);
        tabItem.setIndex(tabItemList.size());
        tabItemList.add(tabItem);
    }

    @Override
    public void onClick(View view) {
        TabItem tabItem = (TabItem) view.getTag();
        if (!interceptTab(tabItem)) {
            Class itemClz = tabItem.getFragmentClass();
            if (itemClz != null) {
                switchFragment(tabItem.index, itemClz);
            } else {
                ToastUtils.showLong("tab没有被拦截，fragmentClass也没有设置");
            }
        }
    }

    private void showFragmentContainer() {
        fragmentContainer.setVisibility(VISIBLE);
    }

    private void hideFragmentContainer() {
        fragmentContainer.setVisibility(GONE);
    }

    private Fragment lastFragment;

    protected void fragmentShow(int index, Class fragmentClass) {

    }

    protected void fragmentHide(int index, Class fragmentClass) {

    }

    protected void switchFragment(int index, Class itemClz) {
        Fragment fragment = null;
        FragmentTransaction transaction = ((Activity) context).getFragmentManager().beginTransaction();
        if (fragmentMap.containsKey(itemClz.getName())) {
            //如果有，说明已经创建了Fragment
            fragment = fragmentMap.get(itemClz.getName());
            if (fragment.isVisible()) {
                transaction.hide(fragment);
                fragmentHide(index, fragment.getClass());
                hideFragmentContainer();
            } else {
                if (lastFragment != null && lastFragment != fragment) {
                    transaction.hide(lastFragment);
                    fragmentHide(index, lastFragment.getClass());
                }
                transaction.show(fragment);
                fragmentShow(index, fragment.getClass());
                lastFragment = fragment;
                showFragmentContainer();
            }
        } else {
            try {
                fragment = (Fragment) itemClz.newInstance();
                fragmentMap.put(itemClz.getName(), fragment);
                int id = 1101101101;
                transaction.add(id, fragment);
                fragmentShow(index, fragment.getClass());
                if (lastFragment != null) {
                    transaction.hide(lastFragment);
                    fragmentHide(index, lastFragment.getClass());
                }
                lastFragment = fragment;
                showFragmentContainer();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
        transaction.commit();
    }

    protected abstract boolean interceptTab(TabItem tabItem);

    public static class TabItem {
        private int index;
        private String tabName;
        private Class fragmentClass;
        private TextView bindTextView;

        public TextView getBindTextView() {
            return bindTextView;
        }

        public void setBindTextView(TextView bindTextView) {
            this.bindTextView = bindTextView;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public String getTabName() {
            return tabName;
        }

        public void setTabName(String tabName) {
            this.tabName = tabName;
        }

        public Class getFragmentClass() {
            return fragmentClass;
        }

        public void setFragmentClass(Class fragmentClass) {
            this.fragmentClass = fragmentClass;
        }
    }
}
